package com.xuzao.developer.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xuzao.developer.base.model.DevelopPlan;
import com.xuzao.developer.base.model.ProjectVersion;
import com.xuzao.developer.base.model.SysUser;
import com.xuzao.developer.dao.DevelopPlanDao;
import com.xuzao.developer.dao.ProjectVersionDao;
import com.xuzao.developer.dao.SysUserDao;
import com.xuzao.developer.dto.DevelopPlanDTO;
import com.xuzao.developer.dto.SysUserDTO;
import com.xuzao.developer.enums.DevelopPlanStatusEnum;
import com.xuzao.developer.utils.LoginUserInfoUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/index/")
public class IndexController {
    @Resource
    private ProjectVersionDao projectVersionDao;
    @Resource
    private DevelopPlanDao developPlanDao;
    @Resource
    private SysUserDao sysUserDao;

    @RequestMapping("login")
    public String login(Model model,String userName,String passWord, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        try {
            if (session != null && session.getAttribute("user") != null) {
                response.sendRedirect("/index/content");
            }
            if (StringUtils.isBlank(userName) && StringUtils.isBlank(passWord)) {
                model.addAttribute("msg","");
                return "/common/login";
            }
            if ((StringUtils.isNotBlank(userName) && StringUtils.isBlank(passWord))
                    || (StringUtils.isNotBlank(passWord) && StringUtils.isBlank(userName))){
                model.addAttribute("msg","用户名或密码错误");
                return "/common/login";
            }

            SysUserDTO sysUserDTO = new SysUserDTO();
            sysUserDTO.setUserName(userName);
            sysUserDTO.setPassWord(DigestUtils.md2Hex(passWord));
            List<SysUser>  sysUsers = sysUserDao.list(sysUserDTO,null).getList();
            if (!sysUsers.isEmpty()){
                session.setAttribute("user",sysUsers.get(0));
                response.sendRedirect("/index/content");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        model.addAttribute("msg","用户名或密码错误");
        return "/common/login";
    }

    @RequestMapping("logOut")
    public void loginOut(HttpServletRequest request,HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        session.setAttribute("user",null);
        try {
            response.sendRedirect("/");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("content")
    public String index(Model model) {
        model.addAttribute("realName", LoginUserInfoUtil.getLoginUser().getRealName());
        return "/common/index_tree";
    }

    @RequestMapping("main")
    public String main(Model model) {
        List<ProjectVersion> projectVersionList = projectVersionDao.selectListAll();
        model.addAttribute("list", projectVersionList);
        return "/common/main";
    }

    @RequestMapping("main_status_count")
    public String mainStatusCount(Long projectId, Model model) {
        DevelopPlanDTO developPlanDTO = new DevelopPlanDTO();
        developPlanDTO.setProjectId(projectId);
        List<DevelopPlan> developPlans = developPlanDao.list(developPlanDTO, null).getList();
        Map<Integer, List<DevelopPlan>> map = Maps.newHashMap();
        if (!developPlans.isEmpty()) {
            map = developPlans.stream().collect(Collectors.groupingBy(DevelopPlan::getStatus));
        }
        List<Map<Object, Object>> rtn = Lists.newArrayList();
        for (DevelopPlanStatusEnum value : DevelopPlanStatusEnum.values()) {
            List<DevelopPlan> list = map.get(value.getCode());
            Map<Object, Object> m = Maps.newHashMap();
            m.put("name", value.getDesc());
            if (null != list) {
                m.put("value", list.size());
            } else {
                m.put("value", 0);
            }
            rtn.add(m);
        }
        model.addAttribute("str", JSON.toJSONString(rtn));
        return "/common/echarts-barData";
    }

}
